from google.appengine.ext import db
from Common.CompanyOrg import Company
from Common.GroupOrg import Group
from Common.DuplicatedInstanceError import DuplicatedInstanceError

class Client(db.Model):
    ClientName = db.StringProperty(required=True)
    Ref_Company = db.ReferenceProperty(Company, collection_name='Clients')
    Ref_Group = db.ReferenceProperty(Group, collection_name='Clients')
    
    def put (self):
        # Make sure e-mails are unique for each user
        if (not self.is_saved()) and Client.get_by_key_name(self.ClientName, self._parent_key):
            raise DuplicatedInstanceError ('Client.ClientName', self.ClientName)
 
        # call the parent method
        db.Model.put (self)
        
    def TransactionPut(self):
        db.run_in_transaction(self.put)